//-----------------Sonic CD DrillBot Script-------------------//
//------------------Scripted by makasinaaaa-------------------//
// Yeah...

// Aliases
#alias Object.Value0		:	Object.Timer
#alias Object.Value1		:	Object.ExtraTimer
#alias Object.Value2		:	Object.JumpBlock
#alias Object.Frame			:	Object.WalkTimer
#alias Object.Value3		:	Object.XVelocity
#alias Object.Value4		:	Object.YVelocity
#alias Object.Value5		:	Object.XOriginPos
#alias Object.Value6		:	Object.YOriginPos

#alias Object.PropertyValue	:	Object.Quality


// States
#alias 0	:	SNAKE_IDLE
#alias 1	:	SNAKE_UP
#alias 2	:	SNAKE_UP2
#alias 3	:	SNAKE_SHOOT
#alias 4	:	SNAKE_DOWN
#alias 5	:	SNAKE_COOLDOWN

// Collision Sides
#alias 0	:	CSIDE_FLOOR
#alias 1	:	CSIDE_LWALL
#alias 2	:	CSIDE_RWALL

// Badnik Quality / Property Values
#alias 0	:	GOOD_QUALITY
#alias 1	:	BAD_QUALITY

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

sub ObjectMain
	CallFunction(StageSetup_CheckGoodFuture)	// Check if this should be a flower
	if Object.Quality == GOOD_QUALITY
		switch Object.State
			case SNAKE_IDLE
				if Player.XPos >= Object.XPos
					Object.Direction = FACING_LEFT
				else
					Object.Direction = FACING_RIGHT
				end if
				Object.YVelocity = -350000
				break
			case SNAKE_UP
				Object.YVelocity -= 40000
				Object.YPos += Object.YVelocity
				if Object.YVelocity <= -590000
					Object.State = SNAKE_UP2
					Object.YPos += -200000
				end if
				break
			case SNAKE_UP2
				if Player.XPos >= Object.XPos
					Object.Direction = FACING_LEFT
				else
					Object.Direction = FACING_RIGHT
				end if
				Object.Timer++
				if Object.Timer >= 6
					Object.State = SNAKE_SHOOT
					Object.Timer = 1
				end if
				break
			case SNAKE_SHOOT
				Object.ExtraTimer++
				Object.Timer--
				if Object.Timer == 0
					PlayStageSfx(2, false)
					TempValue1  = Object.XPos

					TempValue2  = Object.YPos
					TempValue2 += -0x20000
					if Object.Direction == FACING_LEFT
						TempValue1 += 0x96000
					else
						TempValue1 += -0x96000
					end if
					CreateTempObject(TypeName[SnakeBullet], TempValue0, TempValue1, TempValue2)
					Object[TempObjectPos].DrawOrder =  3
					if Object.Direction == FACING_LEFT
						Object[TempObjectPos].PropertyValue =  1
					else
						Object[TempObjectPos].PropertyValue =  0
					end if
					TempValue0++
				end if
					if Object.ExtraTimer == 120
						Object.ExtraTimer = 0
						Object.Timer = 0
						Object.State = SNAKE_DOWN
						Object.YVelocity = 350000
					end if
				break
			case SNAKE_DOWN
				if Object.YVelocity > 590000
					Object.YPos += -400000
					Object.State = SNAKE_COOLDOWN
					Object.Timer = 0
				else
					Object.YVelocity += 40000
					Object.YPos += Object.YVelocity
				end if
				break
			case SNAKE_COOLDOWN
				Object.Timer++
				if Object.Timer >= 60
					Object.YPos -= 30000
					Object.State = SNAKE_IDLE
					Object.Timer = 0
				end if
				break
		end switch
	else
		switch Object.State
			case SNAKE_IDLE
				if Player.XPos >= Object.XPos
					Object.Direction = FACING_LEFT
				else
					Object.Direction = FACING_RIGHT
				end if
				Object.YVelocity = -120000
				break
			case SNAKE_UP
				Object.YVelocity -= 20000
				Object.YPos += Object.YVelocity
				if Object.YVelocity <= -340000
					Object.State = SNAKE_UP2
					Object.YPos += -200000
				end if
				break
			case SNAKE_UP2
				if Player.XPos >= Object.XPos
					Object.Direction = FACING_LEFT
				else
					Object.Direction = FACING_RIGHT
				end if
				Object.Timer++
				if Object.Timer >= 12
					Object.State = SNAKE_SHOOT
					Object.Timer = 1
				end if
				break
			case SNAKE_SHOOT
				Object.ExtraTimer++
					if Object.ExtraTimer == 120
						Object.ExtraTimer = 0
						Object.Timer = 0
						Object.State = SNAKE_DOWN
						Object.YVelocity = 120000
					end if
				break
			case SNAKE_DOWN
				if Object.YVelocity > 350000
					Object.YPos += -100000
					Object.State = SNAKE_COOLDOWN
					Object.Timer = 0
				else
					Object.YVelocity += 20000
					Object.YPos += Object.YVelocity
				end if
				break
			case SNAKE_COOLDOWN
				Object.Timer++
				if Object.Timer >= 60
					Object.YPos -= 60000
					Object.State = SNAKE_IDLE
					Object.Timer = 0
				end if
				break
		end switch
	end if
end sub


sub ObjectPlayerInteraction
	switch Object.State
			case SNAKE_IDLE
				PlayerObjectCollision(C_TOUCH, -128, -148, 128, -20) // Left Detect
				if CheckResult == true
					Object.Timer++
					if Object.Timer >= 6
						Object.State = SNAKE_UP
						Object.Timer = 0
					end if
				end if
				break
			case SNAKE_UP
			case SNAKE_UP2
			case SNAKE_SHOOT
			case SNAKE_DOWN
				PlayerObjectCollision(C_TOUCH, -16, -16, 16, 24) // Left Detect
				if CheckResult == true
					CallFunction(Player_BadnikBreak)
				end if
				break
	end switch
end sub

sub ObjectDraw
	if Object.Quality == GOOD_QUALITY
		switch Object.State
			case SNAKE_IDLE
			case SNAKE_UP
			case SNAKE_DOWN
				DrawSpriteFX(0, FX_FLIP, Object.XPos, Object.YPos)
				break
			case SNAKE_UP2
				DrawSpriteFX(1, FX_FLIP, Object.XPos, Object.YPos)
				break
			case SNAKE_SHOOT
				DrawSpriteFX(2, FX_FLIP, Object.XPos, Object.YPos))
				break
		end switch
	else
		switch Object.State
			case SNAKE_IDLE
			case SNAKE_UP
			case SNAKE_DOWN
				DrawSpriteFX(3, FX_FLIP, Object.XPos, Object.YPos)
				break
			case SNAKE_UP2
				DrawSpriteFX(4, FX_FLIP, Object.XPos, Object.YPos)
				break
			case SNAKE_SHOOT
				DrawSpriteFX(5, FX_FLIP, Object.XPos, Object.YPos))
				break
		end switch
	end if
end sub

sub ObjectStartup
	LoadSpriteSheet("R2/Objects.gif")

	// Good Quality
	SpriteFrame(-16, -22, 32, 46, 1, 165)			// #0 - Up 1
	SpriteFrame(-16, -15, 32, 40, 1, 124)			// #1 - Up 2
	SpriteFrame(-16, -15, 32, 40, 1, 83)			// #2 - Shoot
	// Bad Quality
	SpriteFrame(-16, -24, 32, 46, 34, 165)			// #0 - Up 1
	SpriteFrame(-16, -20, 32, 40, 34, 124)			// #1 - Up 2
	SpriteFrame(-16, -20, 32, 40, 34, 83)			// #2 - Shoot
	// Used to be below LoadSpriteSheet, moved here for consistency
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Drill Bot]
			Object[ArrayPos0].XOriginPos = Object[ArrayPos0].XPos
			Object[ArrayPos0].YOriginPos = Object[ArrayPos0].YPos
		end if
		ArrayPos0++
	loop
end sub